package com.zheng.hr.rpc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;

import com.zheng.hr.dao.model.HrRecruitPlan;
import com.zheng.hr.dao.mapper.HrRecruitPlanMapper;
import com.zheng.hr.dao.model.HrRecruitRequire;
import com.zheng.hr.rpc.api.HrRecruitPlanService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.hr.rpc.api.HrRecruitRequireService;
import com.zheng.oa.common.exception.OaException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * hr_recruit_plan 招聘计划 服务实现类
 * </p>
 *
 * @author likun
 * @since 2018-01-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
@BaseService
public class HrRecruitPlanServiceImpl extends BaseServiceImpl<HrRecruitPlanMapper, HrRecruitPlan> implements HrRecruitPlanService {
    @Autowired
    private HrRecruitRequireService recruitRequireService;

    @Override
    public boolean insetRecruitPlan(HrRecruitPlan recruitPlan, List<HrRecruitRequire> requireList) throws OaException {
        if (recruitPlan.getStartTime().compareTo(recruitPlan.getEndTime()) > 0) {
            throw new OaException("开始时间不能在结束时间之前！");
        }
        if (insert(recruitPlan)) {
            for (HrRecruitRequire require : requireList) {
                require.setPlanId(recruitPlan.getId());
                if (require.getRealNumber() > require.getNumber()) {
                    throw new OaException("招聘需求中的实际招聘人数不能超过招聘人数！");
                }
                if (!recruitRequireService.insert(require)) {
                    throw new OaException("添加招聘需求失败！");
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean updateRecruitPlan(HrRecruitPlan recruitPlan, List<HrRecruitRequire> requireList) throws OaException {
        if (recruitPlan.getStartTime().compareTo(recruitPlan.getEndTime()) > 0) {
            throw new OaException("开始时间不能在结束时间之前！");
        }
        if (updateById(recruitPlan)) {
            Integer id = recruitPlan.getId();
            EntityWrapper<HrRecruitRequire> recruitRequireEntityWrapper = new EntityWrapper<>();
            recruitRequireEntityWrapper.where("plan_id=" + id);
            if (recruitRequireService.delete(recruitRequireEntityWrapper)) {
                for (HrRecruitRequire require : requireList) {
                    require.setPlanId(recruitPlan.getId());
                    if (require.getRealNumber() > require.getNumber()) {
                        throw new OaException("招聘需求中的实际招聘人数不能超过招聘人数！");
                    }
                    if (!recruitRequireService.insert(require)) {
                        throw new OaException("添加招聘需求失败！");
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteRecruitPlan(String ids) throws OaException {
        String[] idArray = ids.split("-");
        List<Integer> idList = new ArrayList<>();
        for (String idStr : idArray) {
            idList.add(Integer.parseInt(idStr));
        }
        EntityWrapper<HrRecruitRequire> recruitRequireEntityWrapper = new EntityWrapper<>();
        recruitRequireEntityWrapper.in("plan_id", idList);
        if (!recruitRequireService.delete(recruitRequireEntityWrapper)) {
            throw new OaException("删除关联失败！");
        }
        return deleteBatchIds(ids);
    }
}
